package org.hq.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Getter
    @Value("${imagepath}")
    private String imagePath;//从配置文件中获取文件路径

    /**
     * 添加静态资源文件，外部可以直接访问地址
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        try {
            if (!Files.exists(Paths.get(imagePath))) {
                Files.createDirectories(Paths.get(imagePath));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        registry.addResourceHandler(imagePath+"/**")
                .addResourceLocations("file:" + imagePath + "/");
    }
}
